package dj.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public final class DateUtils {

	public static final String DEFAULT_DATE_PATTERN = "yyyy-MM-dd";
	public static final String DEFAULT_DATETIME_PATTERN = "yyyy-MM-dd HH:mm:ss";

	public DateUtils() {
	}

	public static boolean isExistDate(int yyyy, int mm, int dd) {
		Calendar cal = Calendar.getInstance();
		cal.set(yyyy, mm - 1, dd);
		return cal.get(5) == dd && cal.get(2) + 1 == mm && cal.get(1) == yyyy;
	}

	public static Date toDate(int yyyy, int mm, int dd) {
		Calendar cal = Calendar.getInstance();
		cal.set(yyyy, mm - 1, dd);
		return cal.getTime();
	}

	public static String formatDate(Date value) {
		return formatDate(value, "yyyy-MM-dd");
	}

	public static String formatDateTime(Date value) {
		return formatDate(value, "yyyy-MM-dd HH:mm:ss");
	}

	public static String formatDate(Date value, String pattern) {
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		return value != null ? sdf.format(value) : "";
	}

	public static Date toDate(String value) {
		return toDate(value, "yyyy-MM-dd HH:mm:ss");
	}

	public static Date toDate(String value, String pattern) {
		if (value == null)
			return null;
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		Date date = null;
		try {
			date = sdf.parse(value);
		} catch (ParseException ex) {
		}
		return date;
	}
}
